import { ref, reactive } from 'vue'
import { rePassword } from '@/api/manager'
import { Notify,showModel } from '@/composables/util'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import { logout} from '@/api/manager'
export function useRepassword() {
    const drawRef=ref(null)
    const store = useStore()
    const router = useRouter()
    const form = reactive({
        oldpassword: '',
        password: '',
        repassword: '',
    })
    const formRef = ref(null)
    const rules =
    {
        oldpassword: [
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { min: 3, max: 5, message: '密码长度不符合', trigger: 'blur' },
        ],
        password: [
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { min: 3, max: 5, message: '密码长度不符合', trigger: 'blur' },],
        repassword: [
            { required: true, message: '密码不能为空', trigger: 'blur' },
            { min: 3, max: 5, message: '密码长度不符合', trigger: 'blur' },],

    }

    const onSubmit = () => {
        formRef.value.validate(
            (valid) => {
                console.log(valid);
                if (!valid) {
                    return false
                }
                drawRef.value.showLoading()
                rePassword(form)
                    .then((res) => {
                        Notify('修改密码成功，请重新登录')
                        store.dispatch('logout')
                        router.push('/login')
                    })
                    .finally(() => {
                        drawRef.value.closeLoading()
                    })
            }
        )
    }
    const openRepasswordFrom = () => drawRef.value.open()
    return {
        form,
        drawRef,
        formRef,
        rules,
        onSubmit,
        openRepasswordFrom
    }

}
export function ueslogout(){
    const store = useStore()
    const router = useRouter()
    function clogout() {
      
        showModel('是否要退出登录？').then(
            res => {
              console.log('tuituitui');
              logout().finally(
                () => {
                  store.dispatch('logout')
                  // 移除cooike
                  // 清除用户动态
                    // 跳回登录页
                  router.push('/login')
        
                  // 提示成功
                  Notify('退出登录成功')
        
                }
              )
            }
          )

    }
    

return {clogout}
}